#Load the relevant packages in R
library(tidyverse)
library(tigris)
library(sf)
library(mapview)
library(usmap)
library(rjson)
library(censusapi)
library(leaflet)
library(stringdist)
library(measurements)
library(units)
library(pracma)
library(plotly)
library(ggplot2)
library(tidyverse)
library(sf)
library(DT)
library(esquisse)


options(
  tigris_class = "sf",
  tigris_use_cache = TRUE
)
# SET your path here to the covid19analysis folder.
sg_path <- 'P:/SFBI/Restricted Data Library/Safegraph/covid19analysis/'

#SET your path her to the github covid folder
github_path  <- 'GitHub/covid19/' #C:/Users/kfg3z/OneDrive/Documents/

poi_url <- paste0(sg_path,'ca_poi.rds')

essential_designation_path <- 'essential_business_designation.rds' #paste0(github_path,)

#Get path of source file. YOU WILL NEED TO CHANGE THIS.
sourcefile = 'safegraph_process_patterns_functions.R' #paste0(github_path,)

#SJ boundary loading
sj_boundary <- readRDS("sj_boundary.rds")
# Here we load the file paths for the weekly patterns and home panerl summary data for all weeks in 2019 and 2020 starting from March through the first week in June.


##2019 dates ###
#March 4 2019
patterns_W190304 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-03-04-weekly-patterns-ca.rds')

hps_W190304 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-03-04-home-panel-summary.rds')

#March 11 2019
patterns_W190311 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-03-11-weekly-patterns-ca.rds')

hps_W190311 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-03-11-home-panel-summary.rds')

#March 18 2019
patterns_W190318 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-03-18-weekly-patterns-ca.rds')

hps_W190318 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-03-18-home-panel-summary.rds')

#March 25 2019
patterns_W190325 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-03-25-weekly-patterns-ca.rds')

hps_W190325 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-03-25-home-panel-summary.rds')

#April 1 2019
patterns_W190401 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-04-01-weekly-patterns-ca.rds')

hps_W190401 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-04-01-home-panel-summary.rds')


#April 1 2019
patterns_W190408 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-04-08-weekly-patterns-ca.rds')

hps_W190408 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-04-08-home-panel-summary.rds')


#April 15 2019
patterns_W190415 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-04-15-weekly-patterns-ca.rds')

hps_W190415 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-04-15-home-panel-summary.rds')

#April 22 2019
patterns_W190422 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-04-22-weekly-patterns-ca.rds')

hps_W190422 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-04-22-home-panel-summary.rds')

#April 29 2019
patterns_W190429 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-04-29-weekly-patterns-ca.rds')

hps_W190429 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-04-29-home-panel-summary.rds')

#May 6 2019
patterns_W190506 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-05-06-weekly-patterns-ca.rds')

hps_W190506 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-05-06-home-panel-summary.rds')

#May 13 2019
patterns_W190513 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-05-13-weekly-patterns-ca.rds')

hps_W190513 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-05-13-home-panel-summary.rds')

#May 20 2019
patterns_W190520 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-05-20-weekly-patterns-ca.rds')

hps_W190520 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-05-20-home-panel-summary.rds')


#May 27 2019
patterns_W190527 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-05-27-weekly-patterns-ca.rds')

hps_W190527 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-05-27-home-panel-summary.rds')


#June 3 2019
patterns_W190603 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2019-06-03-weekly-patterns-ca.rds')

hps_W190603 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2019-06-03-home-panel-summary.rds')

##2020 dates ###
#March 2 2020
patterns_W200302 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-03-02-weekly-patterns-ca.rds')

hps_W200302 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-03-02-home-panel-summary.rds')

#March 9 2020
patterns_W200309 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-03-09-weekly-patterns-ca.rds')

hps_W200309 <- paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-03-09-home-panel-summary.rds')

#March 16 2020
patterns_W200316 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-03-16-weekly-patterns-ca.rds')
hps_W200316 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-03-16-home-panel-summary.rds')

#March 23 2020
patterns_W200323 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-03-23-weekly-patterns-ca.rds')
hps_W200323 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-03-23-home-panel-summary.rds')
 
#March 30 2020
patterns_W200330 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-03-30-weekly-patterns-ca.rds')
hps_W200330 <-
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-03-30-home-panel-summary.rds')

#April 6 2020
patterns_W200406 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-04-06-weekly-patterns-ca.rds')
hps_W200406 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-04-06-home-panel-summary.rds')

#April 13 2020
patterns_W200413 <-
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-04-13-weekly-patterns-ca.rds')
hps_W200413 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-04-13-home-panel-summary.rds')

#April 20 2020
patterns_W200420 <- 
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-04-20-weekly-patterns-ca.rds')
hps_W200420 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-04-20-home-panel-summary.rds')

#April 27 2020
patterns_W200427 <- 
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-04-27-weekly-patterns-ca.rds')
hps_W200427 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-04-27-home-panel-summary.rds')


#May 4 2020
patterns_W200504 <- 
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-05-04-weekly-patterns-ca.rds')
hps_W200504 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-05-04-home-panel-summary.rds')

#May 11 2020
patterns_W200511 <- 
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-05-11-weekly-patterns-ca.rds')
hps_W200511 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-05-11-home-panel-summary.rds')

#May 18 2020
patterns_W200518 <- 
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-05-18-weekly-patterns-ca.rds')
hps_W200518 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-05-18-home-panel-summary.rds')

#May 25 2020
patterns_W200525 <- 
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-05-25-weekly-patterns-ca.rds')
hps_W200525 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-05-25-home-panel-summary.rds')

#June 1 2020
patterns_W200601 <- 
  paste0(sg_path,'weekly-patterns/v2/main-file/2020-06-01-weekly-patterns-ca.rds')

hps_W200601 <- 
  paste0(sg_path,'weekly-patterns/v2/home-summary-file/2020-06-01-home-panel-summary.rds')
#Next we loadd the normalization function that was pre-created to normalizes #the visits in the safegraph data to account for the region's population.

#You can assume that the file is in the same directory as your source code.
source(sourcefile)
#NOTE: Per your data sharing agreement, do not save raw Safegraph data outside the P Drive Restricted Data Library. 

# In the code below, we load the weekly pattern data, subset to San Jose, and append latitude, longitude, and NAICS code. See https://docs.safegraph.com/docs/weekly-patterns for explanation of the data. 

poi_ca <- readRDS(poi_url)

essential_designation  <- readRDS(essential_designation_path) 
get_businesses <- function(patterns, home_panel_summary){
#Load the SafeGraph places dataset.


#Load the SafeGraph patterns dataset.
sg_date <- readRDS(patterns)


#Load the SafeGraph home panel summary.
hps_date <- readRDS(home_panel_summary)


#Subset to SJ
sg_sj_businesses <- 
  sg_date %>% 
  filter(city == "San Jose") %>% 
  left_join(
    poi_ca %>% 
      dplyr::select(
        safegraph_place_id,
        latitude,
        longitude,
        top_category,
        sub_category
      ),
    by = "safegraph_place_id"
  ) 

daily_visits <- process_patterns_daily(sg_sj_businesses,hps_date ) %>%
  mutate(
    visit_counts_high = round(visit_counts_high, digits = 2),
    visit_counts_low = round(visit_counts_low, digits = 2)
  )

return(daily_visits)
}

Daily Visits to San Jose Locations

#Processes the daily visits to businesses since March and combines them into one file. 

# businesses_0302 <- get_businesses(patterns_W200302,hps_W200302)
# businesses_0309 <- get_businesses(patterns_W200309,hps_W200309)
# businesses_0316 <- get_businesses(patterns_W200316,hps_W200316)
# businesses_0323 <- get_businesses(patterns_W200323,hps_W200323)
# businesses_0330 <- get_businesses(patterns_W200330,hps_W200330)
# businesses_0406 <- get_businesses(patterns_W200406,hps_W200406) 
# businesses_0413 <- get_businesses(patterns_W200413,hps_W200413)
# businesses_0420 <- get_businesses(patterns_W200420,hps_W200420)
# businesses_0427 <- get_businesses(patterns_W200427,hps_W200427)
# businesses_0504 <- get_businesses(patterns_W200504,hps_W200504) 
# businesses_0511 <- get_businesses(patterns_W200511,hps_W200511)
# businesses_0518 <- get_businesses(patterns_W200518,hps_W200518)
# businesses_0525 <- get_businesses(patterns_W200525,hps_W200525)
# businesses_0601 <- get_businesses(patterns_W200601,hps_W200601)

#now we bind the weekly businesses from March to June toghether 
# cummulative_businesses <-
#   businesses_0302 %>%
#   rbind(businesses_0309) %>%
#   rbind(businesses_0316) %>%
#   rbind(businesses_0323) %>%
#   rbind(businesses_0330) %>%
#   rbind(businesses_0406) %>%
#   rbind(businesses_0413) %>%
#   rbind(businesses_0420) %>%
#   rbind(businesses_0427) %>%
#   rbind(businesses_0504) %>%
#   rbind(businesses_0511) %>%
#   rbind(businesses_0518) %>%
#   rbind(businesses_0525) %>%
#   rbind(businesses_0601)

#saveRDS(cummulative_businesses,'all_businesses_daily_unfiltered.rds')
cummulative_businesses <- readRDS('all_businesses_daily_unfiltered.rds')
#Next the data is cleaned, and location information and essential designation is appended. 

#Here we load the location data. We then append the sector's essential designation (from the state) to each  location. 

places_by_essential_designation <-
  poi_ca %>%
  left_join(essential_designation, by = "naics_code") %>%
  rename(
    lat = latitude,
    long = longitude
  ) %>%
  mutate(
    essential_designation = ifelse(
      essential_service %in% '1', 
      "Essential",
      "Not Essential")) %>%
  dplyr::select(safegraph_place_id, location_name, street_address, naics_code, top_category,sub_category, essential_designation, lat, long)


#Next we append the location data from above to the daily visits business frame.  
cummulative_businesses_cleaned <- 
  cummulative_businesses %>%
  left_join(places_by_essential_designation, by= "safegraph_place_id") %>%
  # filter(!location_name %in% misnamed_essentials) %>% #filters wholesale grocery stores like Cotsco and walmart and target
  filter(!naics_code %in% "531120") %>% #filters plazas and shoping malls
  filter(!naics_code %in% "712190") %>% #filters parks
  filter(!is.na(location_name)) %>%
   # filter(!is.na(naics_code)) %>% #filters places without a NAICS code
  group_by(safegraph_place_id) %>% 
  mutate(
    Visits = (visit_counts_high + visit_counts_low)/2,
    mov_avg = movavg(Visits,5, type=c("s")),
    Visits = round(signif(Visits,2)),
    mov_avg = round(signif(mov_avg,2))
  ) %>% 
  ungroup() %>% 
  rename(
    Name = location_name,
    Sector = top_category,
    Subsector = sub_category, 
    Address = street_address,
    Designation = essential_designation
  ) %>%
  dplyr::select(safegraph_place_id, date, Name, Address, Sector, Subsector, Visits, mov_avg, Designation, lat, long) %>% 
  arrange(date,desc(Visits)) %>%
  mutate(
    LocationID = paste0(Name, ", ",Address))

saveRDS(cummulative_businesses_cleaned,'business_output.rds')
business_default <- "Target, 450 N Capitol Ave"

#Here we find the moving average for the businesses 
data_city <-
    unique(cummulative_businesses_cleaned$date) %>% 
    map_dfr(function(specificdate){
      
      cummulative_businesses_cleaned_altered <-
        cummulative_businesses_cleaned %>% 
        filter(date == specificdate)
      
      data.frame(
        date = specificdate,
        mean_visit_counts = round(mean(cummulative_businesses_cleaned_altered$mov_avg),digits = 1),
        sd_visit_counts = round(sd(cummulative_businesses_cleaned_altered$mov_avg),digits = 1)
      )
    })

#Here we create a data frame that filters to only the Target selected
data_location <-   
  cummulative_businesses_cleaned %>%
  filter(LocationID %in% business_default) %>%
  rename(
    mov_avg_location  = mov_avg
  ) 
   
#here we find the moving average for the sector containing this Target.    
data_sector <-  
  unique(cummulative_businesses_cleaned$date) %>%
  map_dfr(function(specificdate){
    
        business_daily_visits_altered <-
          cummulative_businesses_cleaned %>%
          filter(Sector == data_location$Sector[1]) %>% 
          filter(date == specificdate)
    
        data.frame(
          date = specificdate,
          mov_avg = round(mean(business_daily_visits_altered$mov_avg),digits = 1)#,
          # sd_visit_counts = round(sd(business_daily_visits_altered$mov_avg),digits = 1)
        )
      })  %>%rename(
    mov_avg_sector = mov_avg
  )

#this subsets the name of the sector to be used in the graph below
sector_name <- data_location$Sector[1] 

#this dataframe combines the city, sector, and location specific data for the graph
daily_visits_data <-
  data_city %>%
  left_join(data_sector, by = "date") %>%
  left_join(data_location, by = "date")


#this section find the ma of the data so that the graph appropriately reflects the number of visits 
max_global_business <-
  cummulative_businesses_cleaned %>% 
  pull(mov_avg) %>% 
  max() + 10

max_temp_business <-
  cummulative_businesses_cleaned %>%
  group_by(date) %>%
  summarise(mean = mean(mov_avg),sd = sd(mov_avg))

max_business <- max(max_temp_business$mean + max_temp_business$sd)+10

specific_business_max <- pmax(max(data_location$mov_avg_location),max(data_sector$mov_avg_sector))+10

new_max_business <- pmax(max_business, specific_business_max)

Here we graph a plot of daily visits to Target on 450 N Capitol Avenue from the start of March through the first week of June. As seen Target has significant visits throughout the duration of shelter-in-place orders. The number of visits to this Target is substantially greater than both average visits to its sector, Department Stores, and the city locations generally.

plot_ly(
    data = daily_visits_data,
    x = ~date
   ) %>%
    add_trace(
      x = c("2020-03-16" %>% as.Date(),"2020-03-16" %>% as.Date()),
      y = c(-10,new_max_business),
      type = "scatter",
      mode = "lines",
      line = list(color = "rgb(63,63,63)",dash = "dot"),
      name = "Santa Clara County Shelter-in-Place Order",
      showlegend=F
    ) %>% 
    add_trace(
      y = ~mean_visit_counts-sd_visit_counts,
      type = "scatter",
      mode = "lines",
      line = list(color = "transparent"),
      name = "City, +/- 1 S.D.",
      showlegend=F
    ) %>% 
    add_trace(
      y = ~mean_visit_counts+sd_visit_counts,
      type = "scatter",
      mode = "lines",
      fill = "tonexty",
      fillcolor = "rgba(0,110,127,0.25)",
      line = list(color = "transparent"),
      name = "City, +/- 1 S.D.",
      showlegend=T
    ) %>% 
    add_trace(
      y = ~mean_visit_counts,
      type = "scatter",
      mode = "lines",
      line = list(color = "rgb(0,0,0)"),
      name = "City, 5-Day Moving Average",
      showlegend=T
    )  %>%
   add_trace(
        y = ~mov_avg_location,
        type = "scatter",
        mode = "lines",
        line = list(color = "#F58311"),
        name = paste0(business_default, ", 5-Day Moving Average"),
        showlegend= T
      ) %>% 
    add_trace(
          y = ~mov_avg_sector,
          type = "scatter",
          mode = "lines",
          line = list(color = "#A4262C"),
          name = paste0(sector_name, ", 5-Day Moving Average"),
          showlegend= T
      )  %>%
    add_trace(
      type = "scatter",
      mode = "lines",
      x = c(max(cummulative_businesses_cleaned$date),max(cummulative_businesses_cleaned$date)),
      y = c(-10,max_global_business+10),
      line = list(color = "#006E7F"),
      name = "Current Map View",
      showlegend=F
    )   %>% 
    layout(
      margin = list(t = 25),
      paper_bgcolor='rgb(255,255,255)', 
      plot_bgcolor='rgb(229,229,229)',
      title =  paste0("Daily Visits to ", business_default),
      titlefont = list(size =15),
      xaxis = list(
        title = "",
        gridcolor = 'rgb(255,255,255)',
        showgrid = TRUE,
        showline = FALSE,
        showticklabels = TRUE,
        tickcolor = 'rgb(127,127,127)',
        ticks = 'outside',
        zeroline = FALSE,
        fixedrange = T,
        range = c(min(cummulative_businesses_cleaned$date),max(cummulative_businesses_cleaned$date)+1)
      ),
      yaxis = list(
        title = "Daily Business Visits",
        gridcolor = 'rgb(255,255,255)',
        showgrid = TRUE,
        showline = FALSE,
        showticklabels = TRUE,
        tickcolor = 'rgb(127,127,127)',
        ticks = 'outside',
        size = 12, 
        zeroline = FALSE,
        fixedrange = T,
        range = c(0,new_max_business)
      ),
      annotations = list(
        xref = "x",
        yref = "y",
        x = "2020-03-16" %>% as.Date(),
        y = new_max_business,
        text = "Shelter-in-Place Order",
        xanchor = "center",
        yanchor = "bottom",
        showarrow = F
      ),
      legend = list(
        xanchor = "left",
        yanchor = "top",
        x = 0,
        y = -0.2,
        bgcolor = 'rgb(255,255,255)'
      )
    ) %>% 
    config(displayModeBar = F) 

Visits to San Jose Locations by Sector

The following map shows the top visits to Department Stores like Target and Ross on May 21, 2020. With the Dashboard, the user can select the date and sector by which they would like to filter.

date_default <- '2020-05-21' #set the default date that you want here
sector_default <-  "Department Stores" #set the default sector you want here

#Here we filter to the desired date and sector, and then select up to the top 100 businesses in that category with the most visits. 
sector_top_visits <-  
  cummulative_businesses_cleaned %>% 
      filter(date == date_default) %>%
      filter(Sector %in% sector_default) %>% 
      dplyr::arrange(desc(Visits)) %>% .[1:100,] %>%
      filter(!is.na(Visits))

#Here we create the map to highlight the businesses with the most visits
leaflet() %>% 
    addProviderTiles(providers$CartoDB.Positron) %>%  
    addPolygons(
      data = sj_boundary,
      fill = F,
      weight = 1,
      color = "black"
     )  %>% 
    addCircleMarkers(
      data =  sector_top_visits, 
      lng = ~long,
      lat = ~lat,
      radius = 3, 
      color = "blue",
      stroke = F,
      fillOpacity = 0.75,
      label = ~paste0(Name,", Daily Visits:", Visits)
    )

The following line graph shows the average daily visits to businesses in five key sectors that generate signicant sales revenue. The sector with the highest visit is Department stores which includes businesses like Target and Ross. The next most visited category is General Merchandise Stores, which includes locations like Walmart and Cotsco.

#Selects sectors that are designated as important and will appear in the graph
key_sectors <- c("Department Stores", "General Merchandise Stores", "Gasoline Stations", "Beer, Wine, and Liquor Stores", "Grocery Stores")


#Here we filter the businesses data frame to daily visits in key sectors, and then we group and summarize daily visits by sector. 
sector_visits <- cummulative_businesses_cleaned %>%
   mutate(
  Sector = ifelse(  #this if statement helps makes the graph more succint
    Sector %in% "General Merchandise Stores, including Warehouse Clubs and Supercenters",
    "General Merchandise Stores",
    Sector
    )
 ) %>%
  group_by(Sector, date) %>%
  summarize(
    mean_mov_avg = round(mean(mov_avg), 0)
  ) %>%
 filter(Sector %in% key_sectors) 
  
 sector_visits_graph <-  
    sector_visits %>%
    spread(Sector, mean_mov_avg) %>%
    rename(
      Liquor = 'Beer, Wine, and Liquor Stores',
      Department = 'Department Stores',
      Merchandise = 'General Merchandise Stores',
      Gas = 'Gasoline Stations', 
      Grocery = 'Grocery Stores'
    )

#saveRDS(sector_visits_graph, 'sj_sector_daily_visits_graph.rds')
#saveRDS(sector_visits, 'sj_sector_daily_visits.rds')

#Creates a plot of daily visits to businesses 
sector_plot <- ggplot(sector_visits) +
 aes(x = date, y = mean_mov_avg, colour = Sector) +
 geom_line(size = 1L) +
 scale_color_hue() +
 labs(x = "Date", y = "Daily Visits (5-day moving average)", title = "Mean Daily Visits to Sectors' Businesses ") +
 theme_minimal()

plot(sector_plot)

2019 vs 2020 Weekly Visits to San Jose Businesses

get_weekly_visits <- function(patterns, home_panel_summary) {
#Load the SafeGraph places dataset.


#Load the SafeGraph patterns dataset.
sg_date <- readRDS(patterns)


#Load the SafeGraph home panel summary.
hps_date <- readRDS(home_panel_summary)

#Subset to SJ
sg_sj_businesses <- 
  sg_date %>% 
  filter(city == "San Jose") %>% 
  left_join(
    poi_ca %>% 
      dplyr::select(
        safegraph_place_id,
        latitude,
        longitude,
        top_category,
        sub_category
      ),
    by = "safegraph_place_id"
  ) 

sg_sj_business_norm <- normBG(sg_sj_businesses, hps_date) 

business_sum <- 
  sg_sj_business_norm %>% 
    group_by(safegraph_place_id) %>% 
    summarize(
      visitor_counts = sum(origin_visitor_counts_high, na.rm=T),
      visit_counts = sum(visit_counts, na.rm=T)
    ) %>% 
    left_join(sg_sj_businesses, by = "safegraph_place_id") %>%
    filter(!is.na(longitude)) %>% 
    mutate(
      long = longitude,
      lat = latitude,
      date_range_start = date_range_start %>%  substr(1,10) %>% as.Date(),
      date_range_end = date_range_end %>%  substr(1,10) %>% as.Date(),
      visit_counts = round(visit_counts, digits = 2)
    ) %>%
  select(
    safegraph_place_id,date_range_start, location_name, top_category, sub_category, visit_counts, lat, long)
  
}
#This chunk gets weekly visit data for 2019 and 2020

# W1_19_0304 <- get_weekly_visits(patterns_W190304,hps_W190304)
# W2_19_0311 <- get_weekly_visits(patterns_W190311,hps_W190311)
# W3_19_0318 <- get_weekly_visits(patterns_W190318,hps_W190318)
# W4_19_0325 <- get_weekly_visits(patterns_W190325,hps_W190325)
# W5_19_0401 <- get_weekly_visits(patterns_W190401,hps_W190401)
# W6_19_0408 <- get_weekly_visits(patterns_W190408,hps_W190408)
# W7_19_0415 <- get_weekly_visits(patterns_W190415,hps_W190415)
# W8_19_0422 <- get_weekly_visits(patterns_W190422,hps_W190422)
# W9_19_0429 <- get_weekly_visits(patterns_W190429,hps_W190429)
# W10_19_0506 <- get_weekly_visits(patterns_W190506,hps_W190506)
# W11_19_0513 <- get_weekly_visits(patterns_W190513,hps_W190513)
#W12_19_0520 <- get_weekly_visits(patterns_W190520,hps_W190520)
# W13_19_0527 <- get_weekly_visits(patterns_W190527,hps_W190527) 
# W14_19_0603 <- get_weekly_visits(patterns_W190603,hps_W190603) 
# 
#  
# W1_20_0302 <- get_weekly_visits(patterns_W200302,hps_W200302)
# W2_20_0309 <- get_weekly_visits(patterns_W200309,hps_W200309)
# W3_20_0316 <- get_weekly_visits(patterns_W200316,hps_W200316)
# W4_20_0323 <- get_weekly_visits(patterns_W200323,hps_W200323)
# W5_20_0330 <- get_weekly_visits(patterns_W200330,hps_W200330)
# W6_20_0406 <- get_weekly_visits(patterns_W200406,hps_W200406)
# W7_20_0413 <- get_weekly_visits(patterns_W200413,hps_W200413)
# W8_20_0420 <- get_weekly_visits(patterns_W200420,hps_W200420)
# W9_20_0427 <- get_weekly_visits(patterns_W200427,hps_W200427)
# W10_20_0504 <- get_weekly_visits(patterns_W200504,hps_W200504)
# W11_20_0511 <- get_weekly_visits(patterns_W200511,hps_W200511)
# W12_20_0518 <- get_weekly_visits(patterns_W200518,hps_W200518)
# W13_20_0525 <- get_weekly_visits(patterns_W200525,hps_W200525) 
# W14_20_0601 <- get_weekly_visits(patterns_W200601,hps_W200601) 
#This chunk joins all of the weekly visits data into one data frame

# cummulative_weekly_visits <-
# W1_19_0304 %>%
# rbind(W2_19_0311) %>%
# rbind(W3_19_0318) %>%
# rbind(W4_19_0325) %>%
# rbind(W5_19_0401) %>%
# rbind(W6_19_0408) %>%
# rbind(W7_19_0415) %>%
# rbind(W8_19_0422) %>%
# rbind(W9_19_0429) %>%
# rbind(W10_19_0506) %>%
# rbind(W11_19_0513) %>%
# rbind(W12_19_0520) %>%
# rbind(W13_19_0527) %>%
# rbind(W14_19_0603) %>%
# rbind(W1_20_0302) %>%
# rbind(W2_20_0309) %>%
# rbind(W3_20_0316) %>%
# rbind(W4_20_0323) %>%
# rbind(W5_20_0330) %>%
# rbind(W6_20_0406) %>%
# rbind(W7_20_0413) %>%
# rbind(W8_20_0420) %>%
# rbind(W9_20_0427) %>%
# rbind(W10_20_0504) %>%
# rbind(W11_20_0511) %>%
# rbind(W12_20_0518) %>%
# rbind(W13_20_0525) %>%
# rbind(W14_20_0601) 
# 
# cummulative_weekly_visits_cleaned <-
#   cummulative_weekly_visits %>%
# rename(
#   week_start = date_range_start
# ) %>%
# mutate(
#    year = substr(as.character(week_start),1,4),
#    week_number = case_when(
#    as.character(week_start) %in% "2019-03-04" ~ 1,
#    as.character(week_start) %in% "2019-03-11" ~ 2 ,
#     as.character(week_start) %in% "2019-03-18" ~ 3,
#     as.character(week_start) %in% "2019-03-25" ~ 4,
#     as.character(week_start) %in% "2019-04-01" ~ 5,
#     as.character(week_start) %in% "2019-04-08" ~ 6,
#     as.character(week_start) %in% "2019-04-15" ~ 7,
#     as.character(week_start) %in% "2019-04-22" ~ 8,
#     as.character(week_start) %in% "2019-04-29" ~ 9,
#     as.character(week_start) %in% "2019-05-06" ~ 10,
#     as.character(week_start) %in% "2019-05-13" ~ 11,
#     as.character(week_start) %in% "2019-05-20" ~ 12,
#     as.character(week_start) %in% "2019-05-27" ~ 13,
#     as.character(week_start) %in% "2019-06-03" ~ 14,
#     as.character(week_start) %in% "2020-03-02" ~ 1,
#     as.character(week_start) %in% "2020-03-09" ~ 2,
#     as.character(week_start) %in% "2020-03-16" ~ 3,
#     as.character(week_start) %in% "2020-03-23" ~ 4,
#     as.character(week_start) %in% "2020-03-30" ~ 5,
#     as.character(week_start) %in% "2020-04-06" ~ 6,
#     as.character(week_start) %in% "2020-04-13" ~ 7,
#     as.character(week_start) %in% "2020-04-20" ~ 8,
#     as.character(week_start) %in% "2020-04-27" ~ 9,
#     as.character(week_start) %in% "2020-05-04" ~ 10,
#     as.character(week_start) %in% "2020-05-11" ~ 11,
#    as.character(week_start) %in% "2020-05-18" ~ 12,
#    as.character(week_start) %in% "2020-05-25" ~ 13,
#     as.character(week_start) %in% "2020-06-01" ~ 14,
#     )) %>%
# select(-c('location_name', 'top_category', 'sub_category',lat, long))
#  
# saveRDS(cummulative_weekly_visits_cleaned, "sj_businesses_weekly_visits_unfiltered")

 cummulative_weekly_visits_cleaned <- readRDS( "sj_businesses_weekly_visits_unfiltered")

 
cummulative_weekly_visits_detailed <-
  cummulative_weekly_visits_cleaned %>%
  #left_join(poi_ca, by= "safegraph_place_id") %>%
  left_join(places_by_essential_designation, by= "safegraph_place_id") %>%
  select(
    safegraph_place_id,location_name, street_address, naics_code, top_category, sub_category, essential_designation, year, week_number, week_start, visit_counts, lat,long) %>%
  rename(
    Name = location_name,
    Visits = visit_counts,
    Sector = top_category,
    Subsector = sub_category, 
    Address = street_address,
    Designation = essential_designation,
    Date = week_start, 
    Week = week_number
  )%>%
  mutate(
    LocationID = paste0(Name, ", ",Address))
   
saveRDS(cummulative_weekly_visits_detailed, "sj_all_businesses_weekly.rds")

cummulative_weekly_visits_detailed <- readRDS("sj_all_businesses_weekly.rds")

Here we graph the difference in weekly visits to Target on 450 N Capitol Ave in 2019 and 2020. In the dashboard, a user can select any business in San Jose. As seen the visits to this particular Target have actually been higher than those in 2019.

data20 <- 
    cummulative_weekly_visits_detailed  %>% 
    filter(year %in% 2020) %>%
    filter(LocationID %in% business_default) #this business default was selected earlier in the daily visits section. Feel free to change it!
  
data19 <- 
    cummulative_weekly_visits_detailed  %>% 
    filter(year %in% 2019) %>%
    filter(LocationID %in% business_default)
  
plot_ly() %>%
    add_trace(
      x = data19$Week,  
      y = data19$Visits, 
      type = 'bar', 
      showlegend = T,
      name = "2019"
    ) %>% 
    add_trace(
      x = data20$Week,  
      y = data20$Visits, 
      type = 'bar', 
      showlegend = T,
      name = "2020"
    )  %>% 
  layout(
    xaxis = list(title = 'Weeks since the beginning of March', title = list(font = 12)), 
    yaxis = list(title = 'Visits Each Week',  title = list(font = 12)),
    title = paste0("Change in Weekly Visits to ",business_default),
    margin = list(t = 40, b = 30),
    barmode = 'group',
    title = list(font = 12)
    ) %>%
  plotly::config(displayModeBar = F)